/**
 * 
 */
package edu.pku.sei.modeler.editparts;

import java.beans.PropertyChangeEvent;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.draw2d.PolylineDecoration;
import org.eclipse.draw2d.RotatableDecoration;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.FontData;
import org.eclipse.swt.widgets.Display;

import edu.pku.sei.modeler.edit.parts.AbstractLinkEditPart;
import edu.pku.sei.modeler.handler.MiddleEndpointLocator;
import edu.pku.sei.modeler.model.structure.MetaShapeElement;

/**
 * @author YanYan
 *
 */
public class DelegationConnectorEditpart extends AbstractLinkEditPart {

	private RotatableDecoration targetDecor;
	
	private Label nameLabel;
	
	public DelegationConnectorEditpart(MetaShapeElement model) {
		super(model);
	}
	
	protected void createEditPolicies() {
		super.createEditPolicies();
	}
	private PolylineConnection getConnection() {
		return (PolylineConnection)getFigure();
	}
	
	
	protected IFigure createFigure() {
		PolylineConnection conn = new PolylineConnection();	
		conn.setLineStyle(1);
		nameLabel = new Label("<<" + "delegate" + ">>");
		
		FontData[] fontdata = { new FontData("Arial", 10, SWT.BOLD) };
		nameLabel.setFont(new Font(Display.getCurrent(), fontdata));
		MiddleEndpointLocator relationshipLocator = new MiddleEndpointLocator(
				conn, new Point(0, 0));
		
		conn.add(nameLabel, relationshipLocator);
		return conn;
	}
	
	private PolylineDecoration createDecoration () {
		PolylineDecoration decoration = new PolylineDecoration();
		decoration.setScale(10, 5);
		return decoration;
	}
	
	
	protected void handlePropertyChanged(PropertyChangeEvent event) {
		
	}
	protected void refreshVisuals() {
		super.refreshVisuals();
		updateTargetDecoration();
	}
	
	private void updateTargetDecoration() {
		if (targetDecor == null) {
			targetDecor = createDecoration();
			getConnection().setTargetDecoration(targetDecor);
		}
	}
}
